<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="Ubuntu低频实用命令, python,machine learning,deep learning,html,css,c,c++,cpp,cmake,ros,linux,ubuntu">
    <meta name="description" content="本文主要介绍了Linux(Ubuntu)系统下低频但是却非常实用的命令">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="referrer" content="no-referrer-when-downgrade">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>Ubuntu低频实用命令 | JackWang&#39;s Blog</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/all.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">

    <script src="/libs/jquery/jquery-3.6.0.min.js"></script>

<meta name="generator" content="Hexo 5.4.2">
<style>.github-emoji { position: relative; display: inline-block; width: 1.2em; min-height: 1.2em; overflow: hidden; vertical-align: top; color: transparent; }  .github-emoji > span { position: relative; z-index: 10; }  .github-emoji img, .github-emoji .fancybox { margin: 0 !important; padding: 0 !important; border: none !important; outline: none !important; text-decoration: none !important; user-select: none !important; cursor: auto !important; }  .github-emoji img { height: 1.2em !important; width: 1.2em !important; position: absolute !important; left: 50% !important; top: 50% !important; transform: translate(-50%, -50%) !important; user-select: none !important; cursor: auto !important; } .github-emoji-fallback { color: inherit; } .github-emoji-fallback img { opacity: 0 !important; }</style>
<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css">
<link rel="stylesheet" href="/css/prism-line-numbers.css" type="text/css"></head>



   <style>
    body{
       background-image: url(https://cdn.jsdelivr.net/gh/Tokisaki-Galaxy/res/site/medias/background.jpg);
       background-repeat:no-repeat;
       background-size: 100% 100%;
       background-attachment:fixed;
    }
</style>



<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">JackWang&#39;s Blog</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="" class="waves-effect waves-light">

      
      <i class="fas fa-book-reader" style="zoom: 0.6;"></i>
      
      <span>博客</span>
      <i class="fas fa-chevron-down" aria-hidden="true" style="zoom: 0.6;"></i>
    </a>
    <ul class="sub-nav menus_item_child ">
      
      <li>
        <a href="/tags">
          
          <i class="fas fa-tags" style="margin-top: -20px; zoom: 0.6;"></i>
          
	  <span>按标签归类文章</span>
        </a>
      </li>
      
      <li>
        <a href="/categories">
          
          <i class="fas fa-bookmark" style="margin-top: -20px; zoom: 0.6;"></i>
          
	  <span>按目录归类文章</span>
        </a>
      </li>
      
      <li>
        <a href="/archives">
          
          <i class="fas fa-archive" style="margin-top: -20px; zoom: 0.6;"></i>
          
	  <span>按日期分类文章</span>
        </a>
      </li>
      
    </ul>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>



<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">JackWang&#39;s Blog</div>
        <div class="logo-desc">
            
            JackWang的个人博客
            
        </div>
    </div>

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="javascript:;">
			
				<i class="fa-fw fas fa-book-reader"></i>
			
			博客
			<span class="m-icon"><i class="fas fa-chevron-right"></i></span>
		</a>
            <ul  style="background:  ;" >
              
                <li>

                  <a href="/tags " style="margin-left:75px">
				  
				   <i class="fa fas fa-tags" style="position: absolute;left:50px" ></i>
			      
                              <span>按标签归类文章</    span>

                  </a>
                </li>
              
                <li>

                  <a href="/categories " style="margin-left:75px">
				  
				   <i class="fa fas fa-bookmark" style="position: absolute;left:50px" ></i>
			      
                              <span>按目录归类文章</    span>

                  </a>
                </li>
              
                <li>

                  <a href="/archives " style="margin-left:75px">
				  
				   <i class="fa fas fa-archive" style="position: absolute;left:50px" ></i>
			      
                              <span>按日期分类文章</    span>

                  </a>
                </li>
              
            </ul>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        
    </ul>
</div>


        </div>

        
    </nav>

</header>

    
<script src="/libs/cryptojs/crypto-js.min.js"></script>
<script>
    (function() {
        let pwd = '';
        if (pwd && pwd.length > 0) {
            if (pwd !== CryptoJS.SHA256(prompt('抱歉，这篇文章并不想让所有人都看到，请输入授权密码观看')).toString(CryptoJS.enc.Hex)) {
                alert('密码错误，将返回主页！');
                location.href = '/';
            }
        }
    })();
</script>




<div class="bg-cover pd-header post-cover" style="background-image: url('https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20220729210010695.png')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">Ubuntu低频实用命令</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/Linux/">
                                <span class="chip bg-color">Linux</span>
                            </a>
                        
                            <a href="/tags/Ubuntu/">
                                <span class="chip bg-color">Ubuntu</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/Linux%E5%AE%9E%E7%94%A8%E6%8A%80%E5%B7%A7/" class="post-category">
                                Linux实用技巧
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2022-07-29
                </div>
                

                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-check fa-fw"></i>更新日期:&nbsp;&nbsp;
                    2023-06-01
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    3.4k
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-clock fa-fw"></i>阅读时长:&nbsp;&nbsp;
                    12 分
                </div>
                

                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">

        

        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20220729210010695.png" alt="Ubuntu官网"></p>
<h1 id="Ubuntu低频实用命令"><a href="#Ubuntu低频实用命令" class="headerlink" title="Ubuntu低频实用命令"></a>Ubuntu低频实用命令</h1><p>最近在工作的时候，总是会遇到这样一个情况，就是在某个特定的场合下，需要用一个具体功能的命令，这个命令之很久之前用过，但是因为最近没怎么用，忘记了命令的格式。于是又不得不去网上搜索，然后找到熟悉的博客，找到丢失的记忆。</p>
<p>久而久之，就觉得很烦，所以决定专门写一个文章，把这些低频而又实用的命令记录下来。</p>
<h2 id="1-软连接：ln-s"><a href="#1-软连接：ln-s" class="headerlink" title="1. 软连接：ln -s"></a>1. 软连接：ln -s</h2><p><code>Linux</code>系统及其发行版系统中，使用的文件系统基本都是<code>ext</code>系列的。而<code>ext</code>文件系统中有一个概念，就是<code>链接</code>。链接可以视为文件或者目录的镜像。直观的理解，有所谓的链接(<code>link</code>)，我们可以将其视为文件或者目录的别名。</p>
<p>链接分<strong>软链接</strong>和<strong>硬链接</strong>两种，常用的是软连接，因为硬链接会导致文件在硬盘中存在多个镜像。</p>
<h3 id="A-创建软连接"><a href="#A-创建软连接" class="headerlink" title="A. 创建软连接"></a>A. 创建软连接</h3><p><code>ln</code>命令可以用于创建软连接或者硬链接，其格式为：</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">ln</span> <span class="token punctuation">[</span>参数<span class="token punctuation">]</span> <span class="token punctuation">[</span>源文件或目录<span class="token punctuation">]</span>  <span class="token punctuation">[</span>目标文件或目录<span class="token punctuation">]</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>创建软连接的格式为：</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">ln</span> -s <span class="token punctuation">[</span>源文件或目录<span class="token punctuation">]</span>  <span class="token punctuation">[</span>目标文件或目录<span class="token punctuation">]</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>例如我现在在当前目录下创建一个指向<code>~/project/data/cifar10</code>的软连接，则命令为：</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">mkdir</span> datasets
<span class="token function">ln</span> -s ~/projects/data/cifar10  ./datasets/cifar10
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<h3 id="B-删除软连接"><a href="#B-删除软连接" class="headerlink" title="B. 删除软连接"></a>B. 删除软连接</h3><p><code>rm</code>命令可以用于删除文件或者目录，软连接自然也可以。</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">rm</span> <span class="token punctuation">[</span>软链接<span class="token punctuation">]</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>但是需要注意的是，删除软连接的时候，不能加<code>/</code>，例如，现在要删除上面创建的<code>cifar10</code>软连接，<strong>那么就要用</strong></p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">rm</span> -r cifar10
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p><strong>注意，不能用</strong>：</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">rm</span> -r cifar10/
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>因为<code>cifar10</code>是一个软连接，如果我们加了<code>/</code>，就会对其进行转义，从而删除掉<code>~/project/data/cifar10/</code>文件夹下的所有文件，但是<code>cifar10</code>这个链接文件本文会被保存下来。</p>
<h3 id="C-查看软连接"><a href="#C-查看软连接" class="headerlink" title="C. 查看软连接"></a>C. 查看软连接</h3><p>我们如果要查看软连接所指向的文件，那么直接用<code>ls</code>命令即可。</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">ls</span> -al ./
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p><code>ls</code>的<code>-l</code>参数会解析软连接，并且显示出软连接指向的文件。</p>
<h2 id="2-压缩和解压缩-tar-gz-tgz"><a href="#2-压缩和解压缩-tar-gz-tgz" class="headerlink" title="2. 压缩和解压缩.tar.gz/.tgz"></a>2. 压缩和解压缩.tar.gz/.tgz</h2><p>压缩文件能够方便的在不同的计算机之间传输数据，亦或者是使用<code>at</code>、<code>crontab</code>等命令定期的备份数据。</p>
<p>压缩、解压<code>zip</code>格式压缩文件很简单，直接<code>zip</code>、<code>unzip</code>即可，而且参数也不多，非常好记。</p>
<p>但是用于压、解压<code>.tar.gz</code>/<code>.tgz</code>格式压缩文件的<code>tar</code>命令的参数很多，总是记不住，所以干脆就记录下来，方便以后查询</p>
<h3 id="A-基本参数"><a href="#A-基本参数" class="headerlink" title="A. 基本参数"></a>A. 基本参数</h3><table align="center">    
  <tbody>        
    <tr>            
      <td>x</td>        
      <td>extract，解压</td>
    </tr>
    <tr>
      <td>c</td>            
      <td>compress，压缩</td>        
    </tr>        
    <tr>            
      <td>z</td>            
      <td>压缩/解压的文件是gzip，即后缀是.tar.gz或者.tgz</td>        
    </tr>        
    <tr>            
      <td>v</td>            
      <td>verbose，显式压缩（解压）过程中压缩（解压）的文件</td>        
    </tr>        
    <tr>            
      <td>f</td>            
      <td>解压的文件名（压缩时生成的压缩文件名）</td>        
    </tr>    
  </tbody>
</table>





<h3 id="B-压缩"><a href="#B-压缩" class="headerlink" title="B. 压缩"></a>B. 压缩</h3><p>压缩直接使用下面的组合</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">tar</span> -czvf 压缩后生成的文件名 要压缩的文件/文件夹名
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>此外，需要注意的是，<code>tar</code>在压缩的时候会保存文件夹的结构，因此我们要压缩一个文件夹的时候，最好在该文件夹外进行压缩。</p>
<p>例如我们要压缩一个名为<code>~/file/test/</code>的文件夹时，使用下面的命令</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">cd</span> ~/file
<span class="token function">tar</span> -czvf test.tar.gz test/
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<p>这样解压完了之后得到<code>test</code>一整个文件夹</p>
<p>但是如果我们按照下面的方式进行压缩</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">cd</span> ~/file/test
<span class="token function">tar</span> -czvf ./*
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<p>那么解压的时候就会得到很多文件</p>
<h3 id="C-解压"><a href="#C-解压" class="headerlink" title="C. 解压"></a>C. 解压</h3><p>解压使用下面的组合</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">tar</span> -xzvf 要解压的文件名
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<h2 id="3-非交互式SSH连接：sshpass"><a href="#3-非交互式SSH连接：sshpass" class="headerlink" title="3. 非交互式SSH连接：sshpass"></a>3. 非交互式SSH连接：sshpass</h2><p>在开发的过程中，很多时候我们都需要干一些重复的事情。因此如果有一些任务在未来一段时间我们重复会干的话，那么我们就可以把这一系列任务写成一个Shell脚本，以后直接调用即可。</p>
<p>而很多时候，我们在本机上完成好了开发之后，需要上传到内网的机器中进行测试。手动的用<code>tar</code>打包完了之后再用<code>scp</code>命令上传到内网服务器中是真的很麻烦。因此我们就想能不能把代码的打包以及上传写成一个脚本。</p>
<p>可是这样干优惠存在一个问题，就是<code>scp</code>或者<code>ssh</code>这样涉及到链接远程服务器的命令都是交互式的，换而言之需要我们手动输入密码，这样就为我们在<code>Shell</code>中集成工作流造成了很大的不便。</p>
<p>因此，我们就在想能不能把密码当成参数传给<code>scp</code>或者<code>ssh</code>，很可惜，这样不行。所以这个时候就有<code>sshpass</code>这个工具来帮助我们了。</p>
<h3 id="A-p-指定密码"><a href="#A-p-指定密码" class="headerlink" title="A. -p 指定密码"></a>A. -p 指定密码</h3><p>使用<code>-p</code>参数直接指定<code>ssh</code>和<code>scp</code>登录时的密码</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token comment" spellcheck="true"># 免密码登录</span>
sshpass -p password <span class="token function">ssh</span> username@host

<span class="token comment" spellcheck="true"># 远程执行命令</span>
sshpass -p password <span class="token function">ssh</span> username@host <span class="token operator">&lt;</span>cmd<span class="token operator">></span>

<span class="token comment" spellcheck="true"># 通过scp上传文件</span>
sshpass -p password <span class="token function">scp</span> local_file root@host:remote_file 

<span class="token comment" spellcheck="true"># 通过scp下载文件</span>
sshpass -p password <span class="token function">scp</span> root@host:remote_file local_file
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>所以组合在一起，我们首先在本地使用<code>tar</code>打包，然后使用<code>scp</code>把压缩好的文件上传到远程服务器上去，接下来使用<code>ssh</code>命令远程执行解压、编译、测试的命令。</p>
<p>类似的，如果想在远程执行多个命令，我们可以先用<code>scp</code>把写好的<code>shell</code>脚本上传到远程服务器上去，然后再使用<code>ssh</code>命令远程执行<code>shell</code>脚本。</p>
<h3 id="B-o-忽略RAS-Key检查"><a href="#B-o-忽略RAS-Key检查" class="headerlink" title="B. -o 忽略RAS Key检查"></a>B. -o 忽略RAS Key检查</h3><p>在第一次链接到远程服务器的时候，第一次认证时，一般会有<code>RSA Key</code>验证的提示：</p>
<pre class="line-numbers language-bash"><code class="language-bash">The authenticity of host ’10.x.x.x <span class="token punctuation">(</span>10.x.x.x<span class="token punctuation">)</span><span class="token string">' can'</span>t be established.
RSA key fingerprint is a4:eb:8c:7d:2a:ef:d6:1c:a3:0c:e8:e5:00:d2:eb:60.
Are you sure you want to <span class="token keyword">continue</span> connecting <span class="token punctuation">(</span>yes/no<span class="token punctuation">)</span>?
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<p>这个时候可以指定<code>-o</code>参数来忽略<code>RSA Key</code>的验证</p>
<pre class="line-numbers language-bash"><code class="language-bash">sshpass -p <span class="token string">"<span class="token variable">$SSH_PASSWD</span>"</span> <span class="token function">ssh</span> root@<span class="token string">"<span class="token variable">$host</span>"</span> -o StrictHostKeyChecking<span class="token operator">=</span>no <span class="token string">"<span class="token variable">$@</span>"</span> 2<span class="token operator">></span>/dev/null
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<h3 id="C-f-从文件中读取密码"><a href="#C-f-从文件中读取密码" class="headerlink" title="C. -f 从文件中读取密码"></a>C. -f 从文件中读取密码</h3><p>使用<code>-f</code>参数从文件中读取密码</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token keyword">echo</span> <span class="token string">"password"</span> <span class="token operator">></span> userpasswd
sshpass -f userpasswd <span class="token function">ssh</span> username@x.x.x.x
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<p>当然可以配合<code>gpg</code>来加密密码，而使用时候把解密后的密码传给<code>sshpass</code>，最后再删掉即可。</p>
<pre class="line-numbers language-bash"><code class="language-bash">gpg -d -q .sshpassword.gpg <span class="token operator">></span> fifo<span class="token punctuation">;</span> sshpass -f fifo <span class="token function">ssh</span><span class="token punctuation">;</span> <span class="token function">rm</span> fifo
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<h3 id="D-e-从环境变量中读取密码"><a href="#D-e-从环境变量中读取密码" class="headerlink" title="D. -e 从环境变量中读取密码"></a>D. -e 从环境变量中读取密码</h3><p>使用<code>-e</code>参数从环境变量中读取密码</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">export</span> SSHPASS<span class="token operator">=</span><span class="token string">"password"</span>
sshpass -e <span class="token function">ssh</span> username@x.x.x.x
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<h2 id="4-测试程序用时：time"><a href="#4-测试程序用时：time" class="headerlink" title="4.  测试程序用时：time"></a>4.  测试程序用时：time</h2><p>很多时候，我们需要测试一个程序、命令运行所花费的时间，为此，我们可以使用<code>Linux</code>中的<code>time</code>命令</p>
<h3 id="A-基本使用"><a href="#A-基本使用" class="headerlink" title="A. 基本使用"></a>A. 基本使用</h3><pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">time</span> 需要计算时间的程序
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>例如我们下面测试<code>ls</code>的用时</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">time</span> <span class="token function">ls</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>其中：</p>
<ul>
<li><code>real</code>时间是指挂钟时间，也就是命令开始执行到结束的时间。这个短时间包括其他进程所占用的时间片，和进程被阻塞时所花费的时间。</li>
<li><code>user</code>时间是指进程花费在用户模式中的CPU时间，这是唯一真正用于执行进程所花费的时间，其他进程和花费阻塞状态中的时间没有计算在内。</li>
<li><code>sys</code>时间是指花费在内核模式中的CPU时间，代表在内核中执系统调用所花费的时间，这也是真正由进程使用的CPU时间。</li>
</ul>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20221109131731051.png" alt="time结果" style="zoom: 50%;"></p>
<p>但是需要注意的是，这里使用的<code>time</code>是<code>shell</code>这个程序的内置命令，而非在<code>PATH</code>中的可执行程序<code>/usr/bin/time</code>，因此，直接使用<code>time</code>的功能有限。</p>
<p><code>shell</code>搜索命令的次序依次为：</p>
<ol>
<li>检测命令是否为<code>alias</code>的命令</li>
<li>对命令进行参数扩展</li>
<li>检测命令是否是<code>shell</code>脚本中定义的函数<code>functions</code></li>
<li>检测命令是否是<code>shell</code>的内建命令<code>BUILTIN commands</code></li>
<li>检测<code>HASH table</code></li>
<li>搜索<code>PATH</code>中是否存在同名程序</li>
</ol>
<p>因此，由于<code>shell</code>的<code>BUILTIN commands</code>就有<code>time</code>，所以如果直接使用的话，用的就是<code>BUILTIN commands</code>而非<code>PATH</code>中的可执行的<code>time</code>二进制文件</p>
<p>为此，可以在<code>time</code>命令前加上<code>\</code>表示强制使用<code>PATH</code>中的命令，则此时</p>
<pre class="line-numbers language-bash"><code class="language-bash">\time <span class="token function">ls</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20221109133352779.png" alt="time结果"></p>
<h3 id="B-f-格式化输出"><a href="#B-f-格式化输出" class="headerlink" title="B. -f 格式化输出"></a>B. -f 格式化输出</h3><p>使用<code>-f</code>选项格式化时间输出：</p>
<pre class="line-numbers language-bash"><code class="language-bash">\time -f <span class="token string">"time: %U"</span> <span class="token function">ls</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20221109133539162.png" alt="time结果"></p>
<p><code>-f</code>选项后的参数：</p>
<div class="table-container">
<table>
<thead>
<tr>
<th>参数</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>%E</code></td>
<td>real时间，显示格式为[小时:]分钟:秒</td>
</tr>
<tr>
<td><code>%U</code></td>
<td>user时间。</td>
</tr>
<tr>
<td><code>%S</code></td>
<td>sys时间。</td>
</tr>
<tr>
<td><code>%C</code></td>
<td>进行计时的命令名称和命令行参数。</td>
</tr>
<tr>
<td><code>%D</code></td>
<td>进程非共享数据区域，以KB为单位。</td>
</tr>
<tr>
<td><code>%x</code></td>
<td>命令退出状态。</td>
</tr>
<tr>
<td><code>%k</code></td>
<td>进程接收到的信号数量。</td>
</tr>
<tr>
<td><code>%w</code></td>
<td>进程被交换出主存的次数。</td>
</tr>
<tr>
<td><code>%Z</code></td>
<td>系统的页面大小，这是一个系统常量，不用系统中常量值也不同。</td>
</tr>
<tr>
<td><code>%P</code></td>
<td>进程所获取的CPU时间百分百，这个值等于 <code>user+system</code> 时间除以总共的运行时间。</td>
</tr>
<tr>
<td><code>%K</code></td>
<td>进程的平均总内存使用量（data+stack+text），单位是 <code>KB</code>。</td>
</tr>
<tr>
<td><code>%w</code></td>
<td>进程放弃CPU的次数，即主动进行上下文切换的次数，例如等待I/O操作完成。</td>
</tr>
<tr>
<td><code>%c</code></td>
<td>进程被抢占CPU的次数，即被迫进行上下文切换的次数（由于时间片到期）。</td>
</tr>
</tbody>
</table>
</div>
<h3 id="C-o-写入到文件中"><a href="#C-o-写入到文件中" class="headerlink" title="C. -o 写入到文件中"></a>C. -o 写入到文件中</h3><p>使用<code>-o</code>选项将执行时间写入到文件中：</p>
<pre class="line-numbers language-bash"><code class="language-bash">\time -o outfile.txt <span class="token function">ls</span>
<span class="token function">cat</span> outfile.txt
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20221109133737939.png" alt="time结果"></p>
<h2 id="5-标准输入作为参数：xargs"><a href="#5-标准输入作为参数：xargs" class="headerlink" title="5. 标准输入作为参数：xargs"></a>5. 标准输入作为参数：xargs</h2><p><code>xargs</code>是 <code>Linux</code> 系统的一个很有用的命令，但是常常被忽视，很多人不了解它的用法。</p>
<h3 id="A-介绍"><a href="#A-介绍" class="headerlink" title="A. 介绍"></a>A. 介绍</h3><p><code>Linux</code> 命令都带有参数，有些命令可以接受<code>标准输入</code>（<code>Stdin</code>）作为参数，例如<code>grep</code>命令：</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">cat</span> /etc/passwd <span class="token operator">|</span> <span class="token function">grep</span> root
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>上面的代码使用了管道命令（<code>|</code>）。管道命令的作用，是将左侧命令（<code>cat /etc/passwd</code>）的<code>标准输出</code>转换为<code>标准输入</code>，提供给右侧命令（<code>grep root</code>）作为参数。</p>
<p>因为<code>grep</code>命令可以接受标准输入作为参数，所以上面的代码等同于下面的代码。</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">grep</span> root /etc/passwd
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p><strong>然而，能够接收标准输入作为参数的命令只是少数，大多数命令都不接受标准输入作为参数，只能直接在命令行输入参数，这导致无法用管道命令传递参数</strong>。例如，<code>echo</code>命令就不接受管道传参。</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token keyword">echo</span> <span class="token string">"hello world"</span> <span class="token operator">|</span> <span class="token keyword">echo</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>上面的代码不会有输出。因为管道右侧的<code>echo</code>不接受管道传来的标准输入作为参数。因此，这就极大地限制了我们使用管道符来连接多个命令，从而实现整个工作流。</p>
<p>因此，如果我们有命令，能够让所有的命令接收标准输入作为参数，那么就可以极大地提高我们的工作效率，而<code>xargs</code>就是这个命令。</p>
<h3 id="B-基本使用"><a href="#B-基本使用" class="headerlink" title="B. 基本使用"></a>B. 基本使用</h3><p><code>xargs</code>命令的基本用法如下</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">xargs</span> <span class="token punctuation">[</span>options<span class="token punctuation">]</span> <span class="token punctuation">[</span>command<span class="token punctuation">]</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>其中：</p>
<ul>
<li><code>options</code>是命令行参数</li>
<li><code>command</code>是将要传入参数的命令</li>
</ul>
<p>如果没有指定<code>command</code>的话，那么默认将运行<code>echo</code>。例如</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">ps</span> aux <span class="token operator">|</span> <span class="token function">grep</span> <span class="token function">grep</span> <span class="token operator">|</span> <span class="token function">xargs</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20221113115938026.png" alt="xargs默认运行echo"></p>
<h3 id="C-p-确认运行"><a href="#C-p-确认运行" class="headerlink" title="C. -p 确认运行"></a>C. -p 确认运行</h3><p>有的时候可能会运行诸如<code>rm</code>等高危命令，因此<code>xargs</code>提供了<code>-p</code>参数来向用户确认是否运行命令</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">ps</span> aux <span class="token operator">|</span> <span class="token function">grep</span> <span class="token function">grep</span> <span class="token operator">|</span> <span class="token function">awk</span> <span class="token string">'{print <span class="token variable">$2</span>}'</span> <span class="token operator">|</span> <span class="token function">xargs</span> -p <span class="token function">kill</span> -9
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>此时输入<code>y/Y</code>来确认运行，其他的字符都视为不运行。</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20221113120705847.png" alt="-p确认运行"></p>
<h3 id="D-t-输出执行的命令"><a href="#D-t-输出执行的命令" class="headerlink" title="D. -t 输出执行的命令"></a>D. -t 输出执行的命令</h3><p><code>-p</code>会阻塞程序，直到用户输入。有的时候我们只希望需要输出一下将要执行的命令，而不阻塞程序运行，这个时候就可以使用<code>-t</code>参数。</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">ps</span> aux <span class="token operator">|</span> <span class="token function">grep</span> <span class="token function">grep</span> <span class="token operator">|</span> <span class="token function">awk</span> <span class="token string">'{print <span class="token variable">$2</span>}'</span> <span class="token operator">|</span> <span class="token function">xargs</span> -t <span class="token function">kill</span> -9
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20221113132018397.png" alt="-t输出执行的命令"></p>
<h3 id="E-I-保存输出到变量"><a href="#E-I-保存输出到变量" class="headerlink" title="E. -I 保存输出到变量"></a>E. -I 保存输出到变量</h3><p>有的时候我们希望能够将标准输出的内容传递给多个命令，例如：</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token keyword">echo</span> dirs.txt <span class="token operator">|</span> <span class="token function">xargs</span> <span class="token punctuation">..</span><span class="token punctuation">..</span> <span class="token keyword">echo</span> dirs_to_make<span class="token punctuation">;</span> <span class="token function">mkdir</span> dirs_to_make
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>这个情况下，解决办法就是我们将前一个命令的输出保存到一个变量中，而后通过取值就可以多次使用前一个命令的输出值。</p>
<p><code>xargs</code>中通过指定<code>-I</code>参数，来指定变量名</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token keyword">echo</span> <span class="token string">"one\ntwo\nthree"</span> <span class="token operator">></span> foo.txt
<span class="token function">cat</span> foo.txt
<span class="token function">cat</span> foo.txt <span class="token operator">|</span> <span class="token function">xargs</span> -I files <span class="token keyword">echo</span> files
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20221113183745670.png" alt="保存输出到变量中"></p>
<p>但是，如果我们想用多次调用的话，使用<code>;</code>链接是不行的，因为<code>shell</code>会认为<code>;</code>前的是一个命令，即如果我们使用下面的命令来尝试运行多个命令</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">cat</span> foo.txt <span class="token operator">|</span> <span class="token function">xargs</span> -I files <span class="token keyword">echo</span> files<span class="token punctuation">;</span> <span class="token keyword">echo</span> files
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>那么在<code>shell</code>中，这就成了两个命令</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">cat</span> foo.txt <span class="token operator">|</span> <span class="token function">xargs</span> -I files <span class="token keyword">echo</span> files
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>和</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token keyword">echo</span> files
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>那么这样，第二个命令中的<code>file</code>自然无法被<code>xargs</code>替换，所以运行结果如下</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20221113184113074.png" alt="运行结果"></p>
<p>所以如果我们想要使用<code>xargs</code>讲输出传给多个命令的话，就要用下面的调用方法</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">cat</span> foo.txt <span class="token operator">|</span> <span class="token function">xargs</span> -I files <span class="token variable">$SHELL</span> -c <span class="token string">'echo files;echo files'</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20221113184533047.png" alt="运行结果"></p>
<h3 id="F-d-指定分隔符"><a href="#F-d-指定分隔符" class="headerlink" title="F. -d 指定分隔符"></a>F. -d 指定分隔符</h3><p><code>xargs</code>默认上一个命令输出的分隔符是空格和换行，而<code>$PATH</code>的分隔符是<code>:</code>。所以这种时候，可以使用<code>xargs</code>的<code>-d</code>参数来指定分隔符。</p>
<p>注意<code>-d</code>参数在<code>macOS</code>上的<code>xargs</code>不支持，不过可以使用<code>awk</code>的<code>RS</code>和<code>ORS</code>来实现类似的替换效果</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token keyword">echo</span> <span class="token variable">$PATH</span> <span class="token operator">|</span> <span class="token function">xargs</span> -d <span class="token string">':'</span> -I p <span class="token function">bash</span> -c <span class="token string">'echo -e "p"'</span> <span class="token operator">></span> searching_path.txt
<span class="token function">cat</span> searchin_path.txt
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20221113190232452.png" alt="运行结果"></p>

                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/about" rel="external nofollow noreferrer">Jack Wang</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://jackwang0107.github.io/2022/07/29/ubuntu-di-pin-shi-yong-ming-ling/">https://jackwang0107.github.io/2022/07/29/ubuntu-di-pin-shi-yong-ming-ling/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/about" target="_blank">Jack Wang</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/tags/Linux/">
                                    <span class="chip bg-color">Linux</span>
                                </a>
                            
                                <a href="/tags/Ubuntu/">
                                    <span class="chip bg-color">Ubuntu</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.png" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/medias/reward/wechat.jpg" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/2022/09/27/add-system-call-for-xv6/">
                    <div class="card-image">
                        
                        <img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20220928233603685.png" class="responsive-img" alt="Add System Call for XV6 (Unix Version 6)">
                        
                        <span class="card-title">Add System Call for XV6 (Unix Version 6)</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            This blog introduces system call function call of XV6 (Unix Version 6) and also teachs how to add a function call
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2022-09-27
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/%E6%9D%82%E9%A1%B9/" class="post-category">
                                    杂项
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/Operating-System/">
                        <span class="chip bg-color">Operating System</span>
                    </a>
                    
                    <a href="/tags/xv6/">
                        <span class="chip bg-color">xv6</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2022/07/27/np-wan-quan-xing-li-lun/">
                    <div class="card-image">
                        
                        <img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20220728153924152.png" class="responsive-img" alt="NP完全性理论">
                        
                        <span class="card-title">NP完全性理论</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            本文主要介绍了NP完全性理论
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2022-07-27
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/" class="post-category">
                                    数据结构与算法
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/NP/">
                        <span class="chip bg-color">NP</span>
                    </a>
                    
                    <a href="/tags/NP-Hard/">
                        <span class="chip bg-color">NP Hard</span>
                    </a>
                    
                    <a href="/tags/NP-Complete/">
                        <span class="chip bg-color">NP Complete</span>
                    </a>
                    
                    <a href="/tags/Algorithm/">
                        <span class="chip bg-color">Algorithm</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>


<script>
    $('#articleContent').on('copy', function (e) {
        // IE8 or earlier browser is 'undefined'
        if (typeof window.getSelection === 'undefined') return;

        var selection = window.getSelection();
        // if the selection is short let's not annoy our users.
        if (('' + selection).length < Number.parseInt('120')) {
            return;
        }

        // create a div outside of the visible area and fill it with the selected text.
        var bodyElement = document.getElementsByTagName('body')[0];
        var newdiv = document.createElement('div');
        newdiv.style.position = 'absolute';
        newdiv.style.left = '-99999px';
        bodyElement.appendChild(newdiv);
        newdiv.appendChild(selection.getRangeAt(0).cloneContents());

        // we need a <pre> tag workaround.
        // otherwise the text inside "pre" loses all the line breaks!
        if (selection.getRangeAt(0).commonAncestorContainer.nodeName === 'PRE' || selection.getRangeAt(0).commonAncestorContainer.nodeName === 'CODE') {
            newdiv.innerHTML = "<pre>" + newdiv.innerHTML + "</pre>";
        }

        var url = document.location.href;
        newdiv.innerHTML += '<br />'
            + '来源: JackWang&#39;s Blog<br />'
            + '文章作者: Jack Wang<br />'
            + '文章链接: <a href="' + url + '">' + url + '</a><br />'
            + '本文章著作权归作者所有，任何形式的转载都请注明出处。';

        selection.selectAllChildren(newdiv);
        window.setTimeout(function () {bodyElement.removeChild(newdiv);}, 200);
    });
</script>


<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/libs/codeBlock/codeShrink.js"></script>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('2'),
            headingSelector: 'h1, h2, h3, h4, h5, h6'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h1, h2, h3, h4, h5, h6').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    

    <div class="container row center-align"
         style="margin-bottom: 15px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2021-2023</span>
            
            <a href="/about" target="_blank">Jack Wang</a>
            <!-- |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a> -->
            <!-- |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a> -->
            <br>
            
                &nbsp;<i class="fas fa-chart-area"></i>&nbsp;站点总字数:&nbsp;<span
                        class="white-color">603.8k</span>
            
            
            
                
            
            
                <span id="busuanzi_container_site_pv">
                &nbsp;|&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;
                    <span id="busuanzi_value_site_pv" class="white-color"></span>
            </span>
            
            
                <span id="busuanzi_container_site_uv">
                &nbsp;|&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;
                    <span id="busuanzi_value_site_uv" class="white-color"></span>
            </span>
            
            <br>

            <!-- 运行天数提醒. -->
            
                <span id="sitetime"> Loading ...</span>
                <script>
                    var calcSiteTime = function () {
                        var seconds = 1000;
                        var minutes = seconds * 60;
                        var hours = minutes * 60;
                        var days = hours * 24;
                        var years = days * 365;
                        var today = new Date();
                        var startYear = "2021";
                        var startMonth = "11";
                        var startDate = "12";
                        var startHour = "0";
                        var startMinute = "0";
                        var startSecond = "0";
                        var todayYear = today.getFullYear();
                        var todayMonth = today.getMonth() + 1;
                        var todayDate = today.getDate();
                        var todayHour = today.getHours();
                        var todayMinute = today.getMinutes();
                        var todaySecond = today.getSeconds();
                        var t1 = Date.UTC(startYear, startMonth, startDate, startHour, startMinute, startSecond);
                        var t2 = Date.UTC(todayYear, todayMonth, todayDate, todayHour, todayMinute, todaySecond);
                        var diff = t2 - t1;
                        var diffYears = Math.floor(diff / years);
                        var diffDays = Math.floor((diff / days) - diffYears * 365);

                        // 区分是否有年份.
                        var language = 'zh-CN';
                        if (startYear === String(todayYear)) {
                            document.getElementById("year").innerHTML = todayYear;
                            var daysTip = 'This site has been running for ' + diffDays + ' days';
                            if (language === 'zh-CN') {
                                daysTip = '本站已运行 ' + diffDays + ' 天';
                            } else if (language === 'zh-HK') {
                                daysTip = '本站已運行 ' + diffDays + ' 天';
                            }
                            document.getElementById("sitetime").innerHTML = daysTip;
                        } else {
                            document.getElementById("year").innerHTML = startYear + " - " + todayYear;
                            var yearsAndDaysTip = 'This site has been running for ' + diffYears + ' years and '
                                + diffDays + ' days';
                            if (language === 'zh-CN') {
                                yearsAndDaysTip = '本站已运行 ' + diffYears + ' 年 ' + diffDays + ' 天';
                            } else if (language === 'zh-HK') {
                                yearsAndDaysTip = '本站已運行 ' + diffYears + ' 年 ' + diffDays + ' 天';
                            }
                            document.getElementById("sitetime").innerHTML = yearsAndDaysTip;
                        }
                    }

                    calcSiteTime();
                </script>
            
            <br>
            
                <span id="icp"><img src="/medias/icp.png"
                                    style="vertical-align: text-bottom;"/>
                <a href="https://beian.miit.gov.cn" target="_blank">陕ICP备2021014294号-1</a>
            </span>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/jackwang0108" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:2232123545@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=2232123545" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 2232123545" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/libs/materialize/materialize.min.js"></script>
    <script src="/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/libs/aos/aos.js"></script>
    <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/js/matery.js"></script>

    

    
        
        <script type="text/javascript">
            // 只在桌面版网页启用特效
            var windowWidth = $(window).width();
            if (windowWidth > 768) {
                document.write('<script type="text/javascript" src="/libs/others/sakura.js"><\/script>');
            }
        </script>
    

    <!-- 雪花特效 -->
    

    <!-- 鼠标星星特效 -->
    

     
        <script src="https://ssl.captcha.qq.com/TCaptcha.js"></script>
        <script src="/libs/others/TencentCaptcha.js"></script>
        <button id="TencentCaptcha" data-appid="xxxxxxxxxx" data-cbfn="callback" type="button" hidden></button>
    

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    <!--腾讯兔小巢-->
    
    

    

    

    
    <script src="/libs/instantpage/instantpage.js" type="module"></script>
    

</body>

</html>
